AUI().use('node','aui-base','liferay-util-window', function(A) {
	
	// show loading-masked
	window[ksdl_namespace + 'showLoadingMask']=function (){
		if (A.one('body').loadingmask == null)
			A.one('body').plug(A.LoadingMask, { background: '#000' });
		A.one('body').loadingmask.show();
	};
	
	// hide loading-masked
	window[ksdl_namespace+'hideLoadingMask']=function (){
		if (A.one('body').loadingmask == null)
			A.one('body').plug(A.LoadingMask, { background: '#000' });
		A.one('body').loadingmask.hide();
	};
	
	// get the list of checked rows in search container
	window[ksdl_namespace+'listCheckedExcept']=function(searchContainerElement, except) {
		var s = [];

		searchContainerElement = A.one('#'+searchContainerElement);

		if (searchContainerElement) {
			searchContainerElement.all('input[type=checkbox]').each(
				function(item, index, collection) {
					var val = item.val();

					if (val && item.get('name') != except && item.get('checked')) {
						s.push(val);
					}
				}
			);
		}

		return s.join(',');
	};
	// get the list of cskd da lap thong bao kiem tra
	window[ksdl_namespace+'dsCSKDDaLapThongBao']=function(checkedList, cskdDangLapThongBao) {
		console.log('checkedList ',checkedList);
		console.log('cskdDangLapThongBao',cskdDangLapThongBao);
		var checkedList = checkedList.split(',');
		var cskdList = 	JSON.parse(cskdDangLapThongBao);
		var result = [];
		var cskdIdList = Object.keys(cskdList);
		for (var i in checkedList){
			var id = checkedList[i].toString();
			if (cskdIdList.indexOf(id) >= 0){
				result.push(cskdList[id]);
			}
		}
		return result.join(', ');
	};
	// Open popup: id, popupTitle, renderLink, callback function
	Liferay.provide(
		    window,
		    ksdl_namespace + 'openPopup',
		        function(id, popUpTitle, renderLink, callback) {
		
			    	Liferay.Util.Window.getWindow({
		        		dialog: {
		        			centered: true,
		        			modal: true,
		        			constrain: true,
		        			destroyOnHide: true
		        		},
		        		id: id,
		        		title: '<b><center>' + popUpTitle + '</center></b>',
		        		uri: renderLink
		            }).after('destroy', callback);
			    	
		        },
		        ['liferay-util-window']
		    );
	
	// Close popup
	Liferay.provide(
		    window,
		    ksdl_namespace + 'closePopup',
		        function(popupIdToClose) {
		
		            var popupDialog = Liferay.Util.Window.getById(popupIdToClose);
		
		            popupDialog.destroy();
		        },
		        ['liferay-util-window']
		    );

	// Refresh portlet
	Liferay.provide(
	        window,
	        ksdl_namespace + 'refreshPortlet',
	        function() {

	            // refreshing the portlet [Liferay.Util.getOpener().]
	            var curPortletBoundaryId = '#p_p_id' + ksdl_namespace;

	            Liferay.Portlet.refresh(curPortletBoundaryId);
	        },
	        ['aui-dialog','aui-dialog-iframe']
	    );
	
	//Load Quan Huyen
	window[ksdl_namespace + 'loadQuanHuyen'] = function(tinhElemName, quanHuyenElemName, phuongXaElemName) {
		
		var tinhId = ksdl_namespace + 'tinhId_';
		
		$.ajax({
			url: loadQuanHuyenURL,
			dataType: 'json',
			type: "POST",
			data: tinhId + '=' + $("#" + ksdl_namespace + tinhElemName).val(),
			success: function(data) {
				$('#' + ksdl_namespace + quanHuyenElemName + ' option').remove();
				
				$('#' + ksdl_namespace + quanHuyenElemName).append('<option selected="selected" value="0">' + Liferay.Language.get('quan-huyen') + '</option>');
				
				$.each(data, function(index, item) {
				    $('#' + ksdl_namespace + quanHuyenElemName).append($("<option/>", {
				        value: item.quanHuyenId,
				        text: item.tenQuanHuyen
				    }));
				});
				
				window[ksdl_namespace + 'loadPhuongXa'](quanHuyenElemName, phuongXaElemName);
			},
			error: function(xhr, textStatus, errorThrown) {
				showPopupConfirm(textStatus, ['OK'], null);
	        }
		});
	};
	
	// Load phuong xa
	window[ksdl_namespace + 'loadPhuongXa'] = function(quanHuyenElemName, phuongXaElemName) {
		
		var quanHuyenId = ksdl_namespace + 'quanHuyenId_';
		
		$.ajax({
			url: loadPhuongXaURL,
			dataType: 'json',
			type: "POST",
			data: 
				quanHuyenId + '=' + $("#" + ksdl_namespace + quanHuyenElemName).val()
			,
			success: function(data) {
				$('#' + ksdl_namespace + phuongXaElemName + ' option').remove();
				
				$('#' + ksdl_namespace + phuongXaElemName).append('<option selected="selected" value="0">' + Liferay.Language.get('phuong-xa') +'</option>');
				
				$.each(data, function(index, item) {
				    $('#' + ksdl_namespace + phuongXaElemName).append($("<option/>", {
				        value: item.phuongXaId,
				        text: item.tenPhuongXa
				    }));
				});
			},
			error: function(xhr, textStatus, errorThrown) {
				showPopupConfirm(textStatus, ['OK'], null);
	        }
		});
	};
	
	// load nganh nghe cdk
	window[ksdl_namespace + 'loadNganhNgheCoDK'] = function(nhomNNName, nganhNgheName) {
		
		var nhomNganhNgheId = ksdl_namespace + "nhomNganhNgheId_";
		
		$.ajax({
			url: loadNganhNgheCoDKURL,
			dataType: 'json',
			type: "POST",
			data: 
				nhomNganhNgheId + '=' + $("#" + ksdl_namespace + nhomNNName).val()
			,
			success: function(data) {
				$("#" + ksdl_namespace + nganhNgheName + " option").remove();
				
				$('#' + ksdl_namespace + nganhNgheName).append('<option selected="selected" value="0"/>');
				
				$.each(data, function(index, item) {
				    $('#' + ksdl_namespace + nganhNgheName).append($("<option/>", {
				        value: item.nganhNgheId,
				        text: item.tenNganhNghe
				    }));
				});
			},
			error: function(xhr, textStatus, errorThrown) {
				showPopupConfirm(textStatus, ['OK'], null);
	        }
		});
	};
	
	window[ksdl_namespace + 'returnNoneIfBlank'] = function(checkString){
		if(checkString === ''){
			return "none";
		} else {
			return checkString;
		}
	};
	
	// validate date
	window[ksdl_namespace + 'validateDate'] = function(date) {
		if(date === ''){
			return false;
		}
		
		return $.datepicker.parseDate('dd/mm/yy', date);
	};
});
